package com.ecactus.spinners;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class CityMainActivity extends AppCompatActivity {
    private Spinner province, city, area;
    private List<String> provinceList;
    private List<String> citylist;
    private List<String> arealist;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_main);

        province = findViewById(R.id.province);
        city = findViewById(R.id.city);
        area = findViewById(R.id.area);

        provinceList = new ArrayList<>();
        citylist = new ArrayList<>();
        arealist = new ArrayList<>();

        //初始化省数据 读取省数据并显示到下拉框
        try {
            InputStream inputStream = getResources().getAssets().open("City.txt");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer = new StringBuffer();
            String lines = bufferedReader.readLine();
            while (lines != null) {
                stringBuffer.append(lines);
                lines = bufferedReader.readLine();
            }

            final JSONArray Data = new JSONArray(stringBuffer.toString());
            //循环这个文件数组、获取数组中每个省对象的名字
            for (int i = 0; i < Data.length(); i++) {
                JSONObject provinceJsonObject = Data.getJSONObject(i);
                String provinceName = provinceJsonObject.getString("name");
                provinceList.add(provinceName);
            }

            //定义省份显示适配器
            ArrayAdapter<String> provinceAdapter = new ArrayAdapter<>(CityMainActivity.this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, provinceList);
            provinceAdapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item);
            province.setAdapter(provinceAdapter);

            province.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    try {
                        //根据当前位置的省份所在的数组位置、获取城市的数组
                        JSONObject provinceObject = Data.getJSONObject(position);
                        final JSONArray cityArray = provinceObject.getJSONArray("city");

                        //更新列表数据
                        if (citylist != null) {
                            citylist.clear();
                        }

                        for (int i = 0; i < cityArray.length(); i++) {
                            JSONObject cityObj = cityArray.getJSONObject(i);
                            String cityName = cityObj.getString("name");
                            citylist.add(cityName);
                        }

                        ArrayAdapter<String> cityAdapter = new ArrayAdapter<>(CityMainActivity.this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, citylist);
                        cityAdapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item);
                        city.setAdapter(cityAdapter);

                        city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                try {
                                    //根据当前城市数组位置 获取地区数据
                                    JSONObject cityJsonObj = cityArray.getJSONObject(position);
                                    JSONArray areaJsonArray = cityJsonObj.getJSONArray("area");

                                    if (arealist != null) {
                                        arealist.clear();
                                    }

                                    for (int i = 0; i < areaJsonArray.length(); i++) {
                                        arealist.add(areaJsonArray.getString(i));
                                    }

                                    ArrayAdapter<String> areaAdapter = new ArrayAdapter<>(CityMainActivity.this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, arealist);
                                    areaAdapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item);
                                    area.setAdapter(areaAdapter);


                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });


        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}